<template>
  <uni-nav-bar title="加注机" color="#333" left-icon="left" @clickLeft="navBack" statusBar></uni-nav-bar>
  <view class="function">
    <view :class="active === 'state' ? 'green' : ''" @click="tabFill('state')">状态</view>
    <view :class="active === 'restart' ? 'green' : ''" @click="tabFill('restart')">重启</view>
  </view>
</template>

<script setup lang="ts">
import { navBack } from '@/utils/navigator';
import { showToast } from '@/utils/prompt';
import { onLoad, onUnload } from '@dcloudio/uni-app';
import { Ref, ref } from 'vue';
import { publish, subscribe, unsubscribe, subject } from '@mqtt';
import type { Subscription } from 'rxjs';

// static
let fillID = null as number; //加注机ID
// data
const active: Ref<'state' | 'restart'> = ref('state'); //state 状态 restart 重启
let stateSubscription: Subscription = null;
let restartSubscription: Subscription = null;

onLoad(({ id }) => {
  fillID= id;
});

onUnload(() => {
  if (stateSubscription !== null) {
    stateSubscription.unsubscribe();
  }
  if (restartSubscription !== null) {
    restartSubscription.unsubscribe();
  }
});

/**
 * 接收信息
 */
function next(data) {
  if (data.IMSI === fillID) {
    const content = active.value === 'state' ? JSON.stringify(data) : '启动成功';
    uni.showModal({
      title: '温馨提示',
      content: content,
      success(res) {
        if (res.confirm) {
          console.log('用户点击确认');
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      },
    });
  }
  delFilling();
}
/**
 * @param {Object} num 执行加注机不同的指令
 */
function tabFill(num) {
  active.value = num;
  const text = num === 'state' ? '获取加注机状态' : '重启加注机';
  uni.showModal({
    title: '温馨提示',
    content: '请确认是否' + text,
    success(res) {
      if (res.confirm) {
        switch (num) {
          case 'state':
            fillState();
            break;
          case 'restart':
            fillRestart();
            break;
        }
      } else if (res.cancel) {
        console.log('用户点击取消');
      }
    },
  });
}
/**
 * 获取加注机状态
 */
function fillState() {
  const topic = 'formal/v1/jzj/index/client/' + fillID;
  const payload = { jsondata: { cmd: 'echo_now' } };
  publish(topic, payload);
  ComeBackFilling();
}
/**
 * 订阅加注机返回信息
 */
function ComeBackFilling() {
  let topic = '';
  switch (active.value) {
    case 'state':
      stateSubscription = subject.subscribe(next);
      topic = 'formal/v1/jzj/cycledata/server';
      subscribe(topic);
      break;
    case 'restart':
      restartSubscription = subject.subscribe(next);
      showToast('发送成功');
      topic = 'formal/v1/jzj/envdata/server';
      subscribe(topic);
      break;
  }
}
/**
 * 取消订阅加注机信息
 */
function delFilling() {
  let topic = '';
  switch (active.value) {
    case 'state':
      stateSubscription.unsubscribe();
      topic = 'formal/v1/jzj/cycledata/server';
      unsubscribe(topic);
      break;
    case 'restart':
      restartSubscription.unsubscribe();
      topic = 'formal/v1/jzj/envdata/server';
      unsubscribe(topic);
      break;
  }
}

/**
 * 重启加注机
 */
function fillRestart() {
  const topic = 'formal/v1/jzj/index/client/' + fillID;
  const payload = { jsondata: { cmd: 'restart' } };
  publish(topic, payload);
  ComeBackFilling();
}
</script>

<style lang="less">
.function {
  width: 95%;
  margin: 2.5%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  view {
    width: 49%;
    height: 136rpx;
    background: #ffffff;
    box-shadow: 0rpx 2rpx 23rpx 1rpx rgba(204, 204, 204, 0.3);
    border-radius: 5px;
    font-size: 32rpx;
    font-family: PingFang SC;
    font-weight: 500;
    color: #333333;
    margin-bottom: 20rpx;
    text-align: center;
    line-height: 136rpx;
  }
  .green {
    background: linear-gradient(-24deg, #2e8df4, #0db5f0);
    box-shadow: 0rpx 2rpx 23rpx 1rpx rgba(204, 204, 204, 0.3);
    color: white;
  }
}
</style>
